﻿using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using NC.Infrastructure.Data;
using NC.Service.CatchManager;
using NC.Service.ContentParams;
using NC.Service.Target;
using NC.Web.Models;
using domainEntity = NC.Cather.ContentParams;
namespace NC.Web.Areas.Admin.Controllers
{
    public class ContentParamsController : Controller
    {
        private readonly IContentParamsService _contentParamsService;
        private readonly ICatchService _catchService;
        private readonly ITargetService _targetService;

        public ContentParamsController(IContentParamsService contentParamsService, ICatchService catchService, ITargetService targetService)
        {
            this._contentParamsService = contentParamsService;
            this._catchService = catchService;
            this._targetService = targetService;
        }

        // GET: ContentParams/Index
        public ActionResult Index(string search = "", int page = 0, string startTime = "", string endTime = "", int targetId = -1)
        {
            if (targetId != -1)
                Session["targetId"] = targetId;
            else if (Session["targetId"] == null)
                return Redirect("/Login");
            else
                targetId = Convert.ToInt32(Session["targetId"]);
            var data = _contentParamsService.GetAll();
            if (!string.IsNullOrEmpty(search))
                data = data.Where(n => n.Id != 0);
            DateTime startTimes, endTimes;
            if (DateTime.TryParse(startTime, out startTimes) && DateTime.TryParse(endTime, out endTimes))
                data = data.Where(n => n.CreatedAt >= startTimes && n.CreatedAt <= endTimes);
            ViewBag.count = data.Count();
            return View(data.PageList(page).ToList());
        }

        // GET: ContentParams/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: ContentParams/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(domainEntity entity)
        {
            var targetId = Convert.ToInt32(Session["targetId"]);
            var target = _targetService.Get(targetId);
            entity.Target = target;
            _contentParamsService.Insert(entity);
            _contentParamsService.Commit();
            return Redirect("/Admin/Target?targetId" + Session["targetId"]);
        }

        // GET: ContentParams/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            domainEntity ContentParams = _contentParamsService.Get(id);
            if (ContentParams == null)
            {
                return HttpNotFound();
            }

            return View(ContentParams);
        }

        // POST: ContentParams/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(domainEntity entity)
        {
            var targetId = Convert.ToInt32(Session["targetId"]);
            var target = _targetService.Get(targetId);
            entity.Target = target;
            _contentParamsService.Update(entity);
            _contentParamsService.Commit();
            return Redirect("/Admin/Target?targetId" + Session["targetId"]);
        }

        // POST: ContentParams/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            string[] ids = id.Split('|');
            int _id;
            foreach (var item in ids)
            {
                _id = Convert.ToInt32(item);
                var _entity = _contentParamsService.Get(_id);
                _contentParamsService.Delete(_entity);
            }
            _contentParamsService.Commit();
            return Json("", JsonRequestBehavior.AllowGet);
        }

        #region Api
        [HttpPost]
        public JsonResult GetTestContent(domainEntity entity, string testUrl = "")
        {
            if (string.IsNullOrEmpty(testUrl))
                return new ApiJson("测试地址不能为空");
            var data = _contentParamsService.GetContent(entity, testUrl, HttpMethod.Get);
            return new ApiJson(data, ResultStatus.Ok);
        }


        #endregion



        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}
